package com.dayuanit.dy16.atm.boatm.controller;

import com.dayuanit.dy16.atm.boatm.dto.ResponseDTO;
import com.dayuanit.dy16.atm.boatm.service.ISalaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class SalaryController extends BaseController {

    @Value("${atm.file.path}")
    private String filePath;

    @Autowired
    private ISalaryService salaryService;

    @RequestMapping("/salary/uploadSalaryFile")
    public ResponseDTO uploadSalaryFile(MultipartFile salaryFile, long salaryId) {

        //上传的文件 保存路径
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String salaryFilePath = filePath + fileName;
        try {
            salaryFile.transferTo(new File(salaryFilePath));

            //更新数据
            salaryService.updateSalaryFile(salaryId, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseDTO.success();
    }

    @RequestMapping("/salary/createSalary")
    public ResponseDTO createSalary(String salaryTitle, String remark) {
        salaryService.createSalary(salaryTitle, remark, getUserId());
        return ResponseDTO.success();
    }

    @RequestMapping("/salary/loadSalarys")
    public ResponseDTO loadSalarys() {
        return ResponseDTO.success(salaryService.loadSalarys());
    }


    /**
     *  接口要考虑 1.版本兼容 2.幂等性(修改操作)
     * @param salaryId
     * @return
     */
    @RequestMapping("/salary/submitVerify")
    public ResponseDTO submitVerify(long salaryId) {
        salaryService.submitVerify(getUserId(), salaryId);
        return ResponseDTO.success();
    }

    /**
     * 大部分情况下 通过角色控制即可
     * 如果想粒度更细一些，可以使用权限直接控制
     * @param salaryId
     * @return
     */
    @RequiresRoles("CFO")
//    @RequiresPermissions("card_manager")
    @RequestMapping("/salary/approved")
    public ResponseDTO approved(long salaryId) {
        salaryService.approved(getUserId(), salaryId);
        return ResponseDTO.success();
    }

    @RequiresRoles("CFO")
    @RequestMapping("/salary/refuse")
    public ResponseDTO refuse(long salaryId) {
        salaryService.refuse(getUserId(), salaryId);
        return ResponseDTO.success();
    }

    @RequestMapping("/salary/querySalaryDetail")
    public ResponseDTO querySalaryDetail(long salaryId) {
        return ResponseDTO.success(salaryService.listDetails(salaryId));
    }
}
